import request from '@/utils/request'

// 查询参数接口
export interface ProductQueryParams {
  pageNum: number
  pageSize: number
  name?: string
  categoryId?: number
  brand?: string
  status?: number
}

// 商品添加请求接口
export interface ProductAddRequest {
  name: string
  description?: string
  categoryId: number
  brand?: string
  model?: string
  price: number
  costPrice?: number
  stockQuantity?: number
  lowStockThreshold?: number
  weight?: number
  volume?: number
  images?: ProductImageRequest[]
}

// 商品图片请求接口
export interface ProductImageRequest {
  imageUrl: string
  imageType?: number
  sortOrder?: number
}

// 商品添加响应接口
export interface ProductAddResponse {
  id: number
  name: string
  description?: string
  categoryId: number
  brand?: string
  model?: string
  status: number
  sortOrder: number
  createdTime: string
  updatedTime: string
}

// 商品列表项接口
export interface ProductListItem {
  id: number
  name: string
  description?: string
  categoryId: number
  categoryName?: string
  brand?: string
  model?: string
  status: number
  statusText?: string
  sortOrder: number
  createdTime: string
  updatedTime: string
}

// 商品详情接口
export interface ProductDetail extends ProductListItem {
  skus?: ProductSku[]
  images?: ProductImage[]
}

// 商品SKU接口
export interface ProductSku {
  id: number
  productId: number
  skuCode: string
  skuName?: string
  price: number
  costPrice?: number
  stockQuantity: number
  lowStockThreshold: number
  weight?: number
  volume?: number
  status: number
  createdTime: string
  updatedTime: string
}

// 商品图片接口
export interface ProductImage {
  id: number
  productId: number
  imageUrl: string
  imageType: number
  sortOrder: number
  createdTime: string
}

// 分页查询商品列表
export const getProductPageApi = (params: ProductQueryParams) => {
  return request.get('/v1/products/page', { params })
}

// 查询商品详情
export const getProductByIdApi = (id: number) => {
  return request.get(`/v1/products/${id}`)
}

// 添加新商品
export const addProductApi = (data: ProductAddRequest) => {
  return request.post('/v1/products', data)
}

// 更新商品
export const updateProductApi = (id: number, data: Partial<ProductAddRequest>) => {
  return request.put(`/v1/products/${id}`, data)
}

// 删除商品
export const deleteProductApi = (id: number) => {
  return request.delete(`/v1/products/${id}`)
}

// 批量删除商品
export const deleteProductsApi = (ids: number[]) => {
  return request.delete('/v1/products/batch', { data: ids })
}

// 上架商品
export const onlineProductApi = (id: number) => {
  return request.put(`/v1/products/${id}/online`)
}

// 下架商品
export const offlineProductApi = (id: number) => {
  return request.put(`/v1/products/${id}/offline`)
}

// 保存商品（兼容旧接口）
export const saveProductApi = (data: any) => {
  return request.post('/v1/products', data)
}
